this.pointVectorDecac = structData("x|y");
// type_vector: X : truong hop x = a voi moi y
// type_vector: Y : truong hop y = b voi moi X
// type_vector: XY : truong hop y = ax + b
this.lineVectorDecac = structData("a|b|length_vector|type_vector");
	
var vectorTool = function() {
	this.PI = 3.14;
	this.USCLN = function(x, y) {
	    if (x % y == 0) return y;
	    var USC = x;
	    while (x != 0 && y != 0) {
	        x = x % y;
	        if (x != 0) {
	            // x : so nho - y : so lon
	            y = y % x;

	            if (y == 0) {
	                USC = x;
	                break;
	            }
	        }
	    }
	    return USC;
	};

	this.BSCNN = function(x, y) {
	    if (x % y == 0) return x;
	    var usc = this.USCLN(x, y);
	    return x * y / usc;
	};

	// tinh do dai vector qua 2 diem
	this.lengthXY = function(point1, point2) {
	    var dLength = 0;
	    var dX = Math.pow(point1.x - point2.x, 2);
	    var dY = Math.pow(point1.y - point2.y, 2);
	    dLength = Math.sqrt(dX + dY);
	    return dLength;
	};

	// duong thang di qua 2 diem point1, point2
	this.createVector = function(point1, point2) {
	    // truong hop x = a voi moi y
	    var vectorLine = null;
	    var a, b, lengthVector;
	    if (point1.x == point2.x) {
	        b = 0;
	        a = point1.x;
	        lengthVector = Math.abs(point1.y - point2.y);
	        vectorLine = new lineVectorDecac(a, b, lengthVector, "X");
	        return vectorLine;
	    }
	    // truong hop y = b voi moi x
	    if (point1.y == point2.y) {
	        a = 0;
	        b = point1.y;
	        lengthVector = Math.abs(point1.x - point2.x);
	        vectorLine = new lineVectorDecac(a, b, lengthVector, "Y");
	        return vectorLine;
	    }

	    // vidu: d1.X = 2 - d1.Y = 3 - d2.X = 3 - di.Y = 4
	    var dBSCNN = this.BSCNN(point1.x, point2.x); // 6
	    var deltaX1 = dBSCNN / point1.x; // 3
	    var deltaX2 = dBSCNN / point2.x; // 2
	    var deltaX1X2 = deltaX1 - deltaX2; // 1
	    var dB = deltaX1 * point1.y - deltaX2 * point2.y; // 3 * 3 - 2 * 4 = 1
	    dB = dB / deltaX1X2; // 1 / 1 = 1

	    var dA = (point1.y - dB) / point1.x;

	    a = dA;
	    b = dB;
	    lengthVector = this.lengthXY(point1, point2);
	    vectorLine = new lineVectorDecac(a, b, lengthVector, "XY");
	    return vectorLine;
	};

	// duong thang di qua 1 diem va song song voi 1 duong khac
	this.createVectorParallel = function(point1, line1) {
	    var vectorLine = null;
	    var a, b, lengthVector;
	    // truong hop x = a voi moi y
	    if (line1.XY == 'X') {
	        b = 0;
	        a = point1.x;
	        lengthVector = 1000; // default
	        vectorLine = new lineVectorDecac(a, b, lengthVector, "X");
	        return vectorLine;
	    }
	    // truong hop y = b voi moi x
	    if (line1.XY == 'Y') {
	        a = 0;
	        b = point1.y;
	        lengthVector = 1000; // default
	        vectorLine = new lineVectorDecac(a, b, lengthVector, "Y");
	        return vectorLine;
	    }
	    a = line1.a;
	    b = point1.y - point1.x * a;
	    lengthVector = 1000; // default
	    vectorLine = new lineVectorDecac(a, b, lengthVector, "XY");
	    return vectorLine;
	};

	// tim giao diem cua 2 duong
	this.findPointFrom2Line = function(line1, line2) {
	    var point = new pointVectorDecac('0', '0');
	    // 2 duong song song
	    if (line1.a == line2.a) {
	        return point;
	    }

	    // vi du: line1: y = 10 - line2: y = 3*x + 5
	    if (line1.a == 0) {
	        point.x = (line1.b - line2.b) / line2.a;
	        point.y = line1.b;
	        return point;
	    }

	    // vi du: line2: y = 3*x + 5 - line1: y = 10
	    if (line2.a == 0) {
	        point.x = (line2.b - line1.b) / line1.a;
	        point.y = line2.b;
	        return point;
	    }

	    // vi du: line1: x = 10 - line2: y = 3*x + 5
	    if (line1.b == 0) {
	        point.x = line1.a;
	        point.y = line2.a * line1.a + line2.b;
	        return point;
	    }

	    // vi du: line2: y = 3*x + 5 - line1: x = 10
	    if (line2.b == 0) {
	        point.x = line2.a;
	        point.y = line1.a * line2.a + line1.b;
	        return point;
	    }

	    // vi du: line1: y = 2*x + 3 - line2: y = 3*x + 5
	    var dBSCNN = this.BSCNN(line1.a, line2.a); // 6
	    var deltaX1 = dBSCNN / line1.a; // 3
	    var deltaX2 = dBSCNN / line2.a; // 2
	    var deltaX1X2 = deltaX1 - deltaX2; // 1
	    // tim y
	    var dY = deltaX1 * line1.b - deltaX2 * line2.b; // 3 * 3 - 2 * 5 = -1
	    dY = dY / deltaX1X2; // -1 / 1 = -1
	    // tim x
	    var dX = (dY - line1.b) / line1.a;
	    // xac dinh toa do diem giao cua 2 duong
	    point.x = dX;
	    point.y = dY;
	    return point;
	};

	// tinh goc cua duong thang so voi truc OX
	this.gocDuongThang = function(vectorLine) {
	    var dX = vectorLine.b * -1 / vectorLine.a;
	    var dY = vectorLine.b;
	    if (vectorLine.type_vector=="Y") {
	        return 0;
	    }
	    if (vectorLine.type_vector == "X") {
	        return 90;
	    }
	    if (dX == 0 && dY == 0) {
	        return 45;
	    }
	    var tan = 0;
	    var goc = 0;
	    tan = dY / dX;
	    // atan=angle*PI/180  
	    goc = Math.atan(tan) * 180 / Math.PI;
	    if (dX < 0 && dY < 0) {
	        goc = 360 - goc;
	    }
	    else if (dX < 0 && dY > 0) {
	        if (goc < 0) goc = goc * (-1) + 180;
	    }
	    else if (dX > 0 && dY < 0) {
	        if (goc < 0) goc = goc * (-1) + 180;
	    }
	    else if (dX > 0 && dY > 0) {
	        goc = 180 - goc;
	    }
	    return goc;
	};

	// xac dinh 1 diem co nam tren duong thang da cho ko
	this.isOnLine = function(point1, line)
	{
		var IsOn = false;
		var valueY = point1.y;
		var valueX = line.a * point1.x + line.b;
		var value = valueY - valueX;
		value = base.roundNumber(value, 0);
		if(value == 0) IsOn = true;
		else IsOn = false;
		return IsOn;
	};

	/****** xac dinh trong tam cua 1 da giac *****/
	// mang chua cac diem toa do
	this.PointArrays = [];
	// tim toa do trung tam X tu 3 diem
	// aa, bb, cc: pointVectorDecac
	this.xAG = function(aa, bb, cc) {
		return (aa.x + bb.x + cc.x)/3;
	};
	
	//tim toa do trung tam Y tu 3 diem
	//aa, bb, cc: pointVectorDecac
	this.yAG = function(aa, bb, cc) {
		return (aa.y + bb.y + cc.y)/3;
	};
	
	// xac dinh vi tri tam tu 3 diem
	//aa, bb, cc: pointVectorDecac
	this.sa = function(aa, bb, cc)
	{
		var tam = (aa.x - bb.x)*(aa.y + bb.y) + (bb.x - cc.x)*(bb.y + cc.y) + (cc.x - aa.x)*(cc.y + aa.y);
		return Math.abs(tam)/2;
	};
	
	// xac dinh toa do tam cua 1 da giac tu n dinh
	this.trongtamDaGiac = function(n) {
		var i = 0;
		var tamX = 0, tamY = 0, tamS = 0;
		var tongX = 0, tongY = 0, tong = 0;
		for(i=1; i<n - 1; i++)
		{
			tamX = this.xAG(this.PointArrays[0], this.PointArrays[i], this.PointArrays[i+1]);
			tamY = this.yAG(this.PointArrays[0], this.PointArrays[i], this.PointArrays[i+1]);
			tamS = this.sa(this.PointArrays[0], this.PointArrays[i], this.PointArrays[i+1]);
			tongX = tongX + tamX * tamS ;
			tongY = tongY + tamY * tamS ;
			tong = tong + tamS ;
		}
		var xG = tongX/tong;
		var yG = tongY/tong;
		var trongtam = new pointVectorDecac(xG, yG);
		return trongtam;
	};
	/*********************************************/
	
	// xac dinh toa do cua 1 diem tu 1 diem cho truoc va goc cua no so voi diem cho truoc va do dai ban kinh
	// tu 1 diem A(x1, y1) -> C(x3, y3) tu 1 goc va do dai cho doan AC cho truoc 
	this.timdiemTuDiem_Goc = function(point, angle, radius) {
		var pointA = point;
		var x1 = pointA.x;
		var y1 = pointA.y;
		var dAngle = angle * this.PI/180;
		var ab = radius * Math.cos(dAngle);
		// xac dinh diem B(x2, y2): AB vuong goc CB
		var x2 = x1 + ab;
		var y2 = y1;
		var pointB = new pointVectorDecac(x2, y2);
		// xac dinh diem C(x3, y3)
		var x3 = x2;
		var y3 = 0;
		y3 = pointA.y + radius * Math.sin(dAngle);
		var pointC = new pointVectorDecac(x3, y3);
		return pointC;
	};
};

var line1 = new lineVectorDecac(2, 3, 100, 'XY');
var line2 = new lineVectorDecac(3, 6, 100, 'XY');
var vectorTool = new vectorTool();

/****** end function *********/